/*
 * @Author: your name
 * @Date: 2022-01-28 10:34:36
 * @LastEditTime: 2022-01-29 11:19:54
 * @LastEditors: LAPTOP-L472H14P
 * @Description: In User Settings Edit
 * @FilePath: \react_front_engineer\src\NodeJs\login&registy\app.js
 */
const express = require('express')
const app = express()
const path = require('path')
const router = require(path.join(__dirname, 'router/user.js'))
const userinfo = require((path.join(__dirname, 'router/userinfo.js')));
const cors = require('cors')
const expressJWT = require('express-jwt') // 解析 token 的中间件
const jwtSecretKey = 'hand'

app.use(express.json())
app.use(cors())

app.use(expressJWT({ secret: jwtSecretKey,algorithms: ['HS256'] }).unless({path: [/^\/api\//] }))
app.use("/api", router)
app.use("/my", userinfo);
// 5.为token添加错误处理中间件
app.use((err,req,res,next)=>{
    console.log(err.name);
    if(err.name === "UnauthorizedError"){
        res.send({masg: "token已过期或不合法"})
    }
})
app.listen('3000', () => {
  console.log('serving is running')
})
